package com.jz.community.moduleauthorization.login.task;

import android.app.Activity;

import com.jz.community.basecomm.constant.Constant;
import com.jz.community.basecomm.net.OkHttpUtil;
import com.jz.community.basecomm.task.ITaskCallbackListener;
import com.jz.community.basecomm.task.RxTask;
import com.jz.community.basecomm.utils.JsonUtils;
import com.jz.community.basecomm.utils.Preconditions;
import com.jz.community.commview.view.widget.ProgressDialogManager;
import com.jz.community.basecomm.bean.LoginInfo;

import java.util.HashMap;

/**
 * Created by Tony.Fan on 2018/3/21 17:47
 * 用户名跟验证码登录
 */
public class GetLoginCodeTask extends RxTask<String, Integer, LoginInfo> {

    private Activity activity;
    private ITaskCallbackListener taskListener;

    public GetLoginCodeTask(Activity activity,
                            ITaskCallbackListener taskListener) {
        super(activity);
        this.activity = activity;
        this.taskListener = taskListener;
    }

    @Override
    protected LoginInfo doInBackground(String... params) {
        // 执行请求
        String result = OkHttpUtil.post(Constant.LOGIN_NEW_CODE_URL, getParam(params[0], params[1]));
        LoginInfo info = null;
        if (!Preconditions.isNullOrEmpty(result)) {
            info = JsonUtils.parseObject(result, LoginInfo.class);
        }
        return info;
    }

    @Override
    protected void onPostExecute(LoginInfo result) {
        // 执行结果回调函数
        taskListener.doTaskComplete(result);
        ProgressDialogManager.dismissProgressDialog();
        super.onPostExecute(result);
    }

    @Override
    protected void onPreExecute() {
        ProgressDialogManager.showDialog(activity);
        super.onPreExecute();
    }

    @Override
    protected void onCancelled() {
        super.onCancelled();
    }

    /**
     * 获取请求所需的参数
     *
     * @param
     * @return
     */
    private HashMap<String, Object> getParam(String mobi, String vercode) {

        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("username", mobi);
        hashMap.put("vercode", vercode);
        hashMap.put("login_type", "1");
        return hashMap;
    }
}